package com.syp.hehe.web.controller;

import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.syp.hehe.domain.vo.FavoriteVo;
import com.syp.hehe.service.FavoriteServiceI;
import com.syp.hehe.util.Constants;
import com.syp.hehe.util.ProcessResult;
import com.syp.hehe.util.SessionInfo;
import com.syp.hehe.util.paging.PageOffSet;

@Controller
@RequestMapping({ "/favorite" })
public class FavoriteController {
	
	private static final Logger logger = LoggerFactory.getLogger(FavoriteController.class);
	
	@Autowired
	private FavoriteServiceI favoriteService;
	
	@RequestMapping(value = "/add/{articleId}")
	@ResponseBody
	public ProcessResult add(@PathVariable(value = "articleId") Long articleId) {
		ProcessResult pr = new ProcessResult();
		try {
			FavoriteVo favorite = new FavoriteVo();
			favorite.setArticleId(articleId);
			favorite.setUuid(UUID.randomUUID().toString());
			
			HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
			SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
			favorite.setUsername(sessionInfo.getUsername());
			this.favoriteService.add(favorite);
			
			favorite = this.favoriteService.getByUnameAndArtid(favorite.getUsername(), articleId);
			pr.setSuccess(true);
			pr.setMessage("收藏成功。");
		} catch (Exception e) {
			logger.error("收藏失败。", e);

			pr.setSuccess(false);
			pr.setMessage("收藏失败。");
		}
		return pr;
	}
	
	@RequestMapping(value = "/del/{articleId}")
	@ResponseBody
	public ProcessResult del(@PathVariable(value = "articleId") Long articleId) {
		ProcessResult pr = new ProcessResult();
		try {			
			HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
			SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
			
			this.favoriteService.delete(sessionInfo.getUsername(), articleId);
			pr.setSuccess(true);
			pr.setMessage("取消收藏成功。");
		} catch (Exception e) {
			logger.error("取消收藏失败。", e);

			pr.setSuccess(false);
			pr.setMessage("取消收藏失败。");
		}
		return pr;
	}
	
	@RequestMapping("/list/after/{referId}")
	@ResponseBody
	public ProcessResult list(Long articleId, @PathVariable(value = "referId") Long referId) {	
		ProcessResult pr = new ProcessResult();
		try {
			PageOffSet<FavoriteVo> pageOffSet = this.favoriteService.getPageOffSetList(articleId, referId, Constants.COMMENT_PIGE_SIZE);

			pr.setSuccess(true);
			pr.setMessage("查询评论列表成功。");
			pr.getRet().put("page", pageOffSet);
		} catch (Exception e) {
			logger.error("查询评论列表失败。", e);

			pr.setSuccess(false);
			pr.setMessage("查询评论列表失败。");
		}
		return pr;
	}
	
	
	
}
